//
//  DateFilterView.swift
//  demo
//
//  Created by toro宇 on 2020/2/16.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

let CellHeight : CGFloat = 44

class DateFilterView: UIView {
    
    // 闭包传递事件
    fileprivate  var selectBlock:((_ selectStr:String?)->Void)?
    fileprivate  var dateAry:[Any] = []
    fileprivate  var defaultStr: String?
    
    lazy var tab = UITableView().then{
        $0.separatorStyle = UITableViewCell.SeparatorStyle.none
        $0.register(UITableViewCell.self, forCellReuseIdentifier: "cells")
    }
    
    lazy var shadowBtn = UIButton().then{
        $0.backgroundColor = UIColor.black
        $0.alpha = 0.6
        $0.rx.tap.subscribe(onNext:{ [weak self] in
            self?.removeFromSuperview()
            if((self?.selectBlock) != nil){
                self?.selectBlock!(nil)
            }
            
        })
    }
    
    
    /// 创建类方法
    ///
    /// - Parameters:
    ///   - dataAry: 数据源
    ///   - defaultStr: 默认选择数据
    ///   - selectBlock: 数据回调
    /// - Returns: DateFilterView
    class func dateFilterView(dataAry:Array<Any>,defaultStr:String?,selectBlock: @escaping (_ selectStr:String?)->Void) -> DateFilterView{
        let dateFilterView = DateFilterView()
        dateFilterView.selectBlock = selectBlock
        dateFilterView.dateAry = dataAry
        dateFilterView.defaultStr = defaultStr
        dateFilterView.initCustomView()
        return dateFilterView
        
    }
    
    func updateDateSource(dateSource:Array<Any>) {
        
    }
    
    func initCustomView() {
        
        self.addSubview(shadowBtn)
        shadowBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(tab)
        tab.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            if(dateAry.count > 4){
                make.height.equalTo(CellHeight * 4)
            }else{
                make.height.equalTo(CellHeight * CGFloat(dateAry.count))
            }
        }
        tab.delegate = self
        tab.dataSource = self
    }
}


extension DateFilterView: UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return dateAry.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cells", for: indexPath)
        for subView in cell.subviews {
            subView.removeFromSuperview()
        }
        
        
        let dic  = dateAry[indexPath.row] as! [String : Any]
        let imgView = UIImageView()
        if (dic["imgName"] != nil) {
            cell.addSubview(imgView)
            imgView.snp.makeConstraints { (make) in
                make.left.equalToSuperview().offset(15)
                make.centerY.equalToSuperview()
                make.size.equalTo(CGSize(width: 20,height: 20))
            }
            imgView.image = UIImage(named: "imgName")
        }
        
        let nameLab = UILabel()
        cell.addSubview(nameLab)
        nameLab.snp.makeConstraints { (make) in
            if(dic["imgName"] != nil){
                make.left.equalTo(imgView.snp_rightMargin).offset(14)
            }else{
                make.left.equalToSuperview().offset(15)
            }
            make.centerY.equalToSuperview()
        }
        nameLab.font = UIFont.systemFont(ofSize: 14)
        
        if(dic["title"] as? String == defaultStr){
            nameLab.textColor = UIColor.red
        }else{
            nameLab.textColor = UIColor.black
        }
        nameLab.text = dic["title"] as? String
        
        let line = UIView()
        cell.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
        line.backgroundColor = UIColor.lightGray
        line.isHidden = false
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return CellHeight
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.removeFromSuperview()
        let dic = self.dateAry[indexPath.row] as? [String:Any]
        guard let block = selectBlock else {
            return
        }
        block(dic?["title"] as? String)
    }
    
}
